home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / base / netkit-b.07a / netkit-b / NetKit-B-0.07A / talk / invite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-20  |  6.1 KB  |  199 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. /*
  35.  * From: @(#)invite.c    5.8 (Berkeley) 3/1/91
  36.  */
  37. char inv_rcsid[] = 
  38.   "$Id: invite.c,v 1.5 1996/07/20 20:59:41 dholland Exp $";
  39.  
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/time.h>
  43. #include <signal.h>
  44. #include <netinet/in.h>
  45. #include <protocols/talkd.h>
  46. #include <errno.h>
  47. #include <setjmp.h>
  48. #include <unistd.h>
  49. #include "talk_ctl.h"
  50. #include "talk.h"
  51.  
  52. static void announce_invite(void);
  53.  
  54. /*
  55.  * There wasn't an invitation waiting, so send a request containing
  56.  * our sockt address to the remote talk daemon so it can invite
  57.  * him 
  58.  */
  59.  
  60. /*
  61.  * The msg.id's for the invitations
  62.  * on the local and remote machines.
  63.  * These are used to delete the 
  64.  * invitations.
  65.  */
  66. int    local_id, remote_id;
  67. void    re_invite(int);
  68. sigjmp_buf invitebuf;
  69.  
  70. void
  71. invite_remote(void)
  72. {
  73.     /*volatile int nfd, read_mask, template;*/
  74.     volatile new_sockt;
  75.     struct itimerval itimer;
  76.     CTL_RESPONSE response;
  77.  
  78.     itimer.it_value.tv_sec = RING_WAIT;
  79.     itimer.it_value.tv_usec = 0;
  80.     itimer.it_interval = itimer.it_value;
  81.     if (listen(sockt, 5) != 0)
  82.         p_error("Error on attempt to listen for caller");
  83. #ifdef MSG_EOR
  84.     /* copy new style sockaddr to old, swap family (short in old) */
  85.     msg.addr = *(struct osockaddr *)&my_addr;  /* XXX new to old  style*/
  86.     msg.addr.sa_family = htons(my_addr.sin_family);
  87. #else
  88.     msg.addr = *(struct sockaddr *)&my_addr;
  89. #endif
  90.     msg.id_num = htonl(-1);        /* an impossible id_num */
  91.     invitation_waiting = 1;
  92.     announce_invite();
  93.     /*
  94.      * Shut off the automatic messages for a while,
  95.      * so we can use the interupt timer to resend the invitation
  96.      */
  97.     end_msgs();
  98.     setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
  99.     message("Waiting for your party to respond");
  100.     signal(SIGALRM, re_invite);
  101.     (void) sigsetjmp(invitebuf, 1);
  102.     while ((new_sockt = accept(sockt, 0, 0)) < 0) {
  103.         if (errno == EINTR)
  104.             continue;
  105.         p_error("Unable to connect with your party");
  106.     }
  107.     close(sockt);
  108.     sockt = new_sockt;
  109.  
  110.     /*
  111.      * Have the daemons delete the invitations now that we
  112.      * have connected.
  113.      */
  114.     current_state = "Waiting for your party to respond";
  115.     start_msgs();
  116.  
  117.     msg.id_num = htonl(local_id);
  118.     ctl_transact(my_machine_addr, msg, DELETE, &response);
  119.     msg.id_num = htonl(remote_id);
  120.     ctl_transact(his_machine_addr, msg, DELETE, &response);
  121.     invitation_waiting = 0;
  122. }
  123.  
  124. /*
  125.  * Routine called on interupt to re-invite the callee
  126.  */
  127. void
  128. re_invite(int ignore)
  129. {
  130.     signal(SIGALRM, re_invite);
  131.  
  132.     message("Ringing your party again");
  133.     current_line++;
  134.     /* force a re-announce */
  135.     msg.id_num = htonl(remote_id + 1);
  136.     announce_invite();
  137.     siglongjmp(invitebuf, 1);
  138. }
  139.  
  140. static    char *answers[] = {
  141.     "answer #0",                    /* SUCCESS */
  142.     "Your party is not logged on",            /* NOT_HERE */
  143.     "Target machine is too confused to talk to us",    /* FAILED */
  144.     "Target machine does not recognize us",        /* MACHINE_UNKNOWN */
  145.     "Your party is refusing messages",        /* PERMISSION_REFUSED */
  146.     "Target machine can not handle remote talk",    /* UNKNOWN_REQUEST */
  147.     "Target machine indicates protocol mismatch",    /* BADVERSION */
  148.     "Target machine indicates protocol botch (addr)",/* BADADDR */
  149.     "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
  150. };
  151. #define    NANSWERS    (sizeof (answers) / sizeof (answers[0]))
  152.  
  153. /*
  154.  * Transmit the invitation and process the response
  155.  */
  156. static void
  157. announce_invite(void)
  158. {
  159.     CTL_RESPONSE response;
  160.  
  161.     current_state = "Trying to connect to your party's talk daemon";
  162.     ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
  163.     remote_id = response.id_num;
  164.     if (response.answer != SUCCESS) {
  165.         if (response.answer < NANSWERS)
  166.             message(answers[response.answer]);
  167.         quit();
  168.     }
  169.     /* leave the actual invitation on my talk daemon */
  170.     ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
  171.     local_id = response.id_num;
  172. }
  173.  
  174. /*
  175.  * Tell the daemon to remove your invitation
  176.  */
  177. void
  178. send_delete(void)
  179. {
  180.  
  181.     msg.type = DELETE;
  182.     /*
  183.      * This is just a extra clean up, so just send it
  184.      * and don't wait for an answer
  185.      */
  186.     msg.id_num = htonl(remote_id);
  187.     daemon_addr.sin_addr = his_machine_addr;
  188.     if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
  189.         (struct sockaddr *)&daemon_addr,
  190.         sizeof (daemon_addr)) != sizeof(msg))
  191.         perror("send_delete (remote)");
  192.     msg.id_num = htonl(local_id);
  193.     daemon_addr.sin_addr = my_machine_addr;
  194.     if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
  195.         (struct sockaddr *)&daemon_addr,
  196.         sizeof (daemon_addr)) != sizeof (msg))
  197.         perror("send_delete (local)");
  198. }
  199.